<?php

namespace App\Http\Middleware;

use App\Models\Visitor;
use Closure;
use Illuminate\Http\Request;

class VerifyMini
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $openid = $request->input('openid', '');
        if (empty($openid)) return response()->json(['status'=>400, 'message' => '缺少openid参数']);

        $visitor = Visitor::where('openid', $openid)->first();
        if (empty($visitor)) return response()->json(['status'=>403, 'message' => '无权访问']);

        $request->visitor = $visitor;
        return $next($request);
    }
}
